;----------------------------------------------------------------------------
; decomp sprite routine
;----------------------------------------------------------------------------

; the algorithm - idea by Roger Levy
;                 ported to 6502 by Roger Levy
;
;
; main structure:
;
;  0                + dt2-9[8]
;  100              + data8[8]
;  101 + common8[1] + dt3-9[8]
;  110 + common8[2] + dt3-9[8]
;  111 + common8[4] + data2[8]
;
; data structure:
;
;  dt2-9:
;
;   0aaaaaaaa     a = data
;   10            $00
;   11            $FF
;
;  data8:
;
;   bbbbbbbb      b = data
;
;  common8:
;
;   cccccccc      c = common byte
;
;  dt3-9:
;
;   0dddddddd     d = data
;   100           $00
;   101           $FF
;   110           common[0]
;   111           common[1]
;
;  data2:
;
;   00            common[0]
;   01            common[1]
;   10            common[2]
;   11            common[3]


REGC=TEMP0


; A=src ... dest tilecount --



;----------------------------------------------------------------------------
;[ decomp ]                                                      [150 bytes]
;----------------------------------------------------------------------------
; input:
;  HL -> data to decompress
;  DE -> buffer to store output
;  B = sprite count
;
; output:
;  A = 1, B = 0, C = ?, DE = DE + B * 8, HL = ?
decomp:
   LD    C, %10000000
   DEC   HL
dc_mainloop
   PUSH  BC
   LD    B, %00011111
   CALL  IsBit
   JR    C, dc_switch
   SET   5, B
   JR    dc_innerloop
dc_switch:
   LD    A, 1
   CALL  IsBit
   JR    C, dc_other
   RES   4, B
   CALL  IsBit
   JR    C, dc_getcommon
   SET   7, B
   JR    dc_innerloop
dc_other:
   ADD   A, A
   CALL  IsBit
   JR    NC, dc_getcommon
   ADD   A, A
   SET   6, B
dc_getcommon:
   PUSH  BC
   PUSH  DE
   LD    B, A
   LD    DE, common_array
dc_getloop:
   CALL  GetByte
   LD    (DE), A
   INC   DE
   DJNZ  dc_getloop
   POP   DE
   POP   BC
;   BIT   6, B
;   JR    NZ, dc_assume0
dc_innerloop:
   BIT   7, B
   JR    NZ, dc_raw
   LD    A, %01000000
   BIT   6, B
   JR    NZ, dc_common
   CALL  IsBit
   JR    NC, dc_raw
   BIT   5, B
   JR    NZ, dc_fill
   RLCA
   CALL  IsBit
   JR    C, dc_common
dc_fill:
   CALL  IsBit
   SBC   A, A
   JR    dc_output
dc_raw:
   CALL  GetByte
   JR    dc_output
dc_common:
   BIT   4, B
   JR    NZ, dc_multiple
dc_assume0:
   XOR   A
dc_multiple:
   CALL  NZ, GetBits
   PUSH  DE
   ADD   A, common_array&255
   LD    E, A
   ADC   A, (common_array>>8)&255
   SUB   E
   LD    D, A
   LD    A, (DE)
   POP   DE
dc_output:
   LD    (DE), A
   INC   DE
   DEC   B
   BIT   3, B
   JR    NZ, dc_innerloop
   POP   AF
   LD    B, A
   DJNZ  dc_mainloop
dc_finish:
   RLC   C
   RET   C
   RLC   (HL)
   JR    dc_finish

IsBit:
   RLC   C
   JR    NC, dc_stay
   INC   HL
dc_stay:
   RLC   (HL)
   RET

GetByte:
   LD    A, %00000001
GetBits:
   CALL  IsBit
   RLA
   JR    NC, GetBits
   RET
